package com.example.concurrentprogramming8016.future.completablefuture;

import java.util.DoubleSummaryStatistics;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * @author liyanhao
 * @Date 2023-01-08 21:36
 * anyOf方法的参数是多个给定的CompletableFuture，当其中的任何一个完成时，方法返回
 * 这个CompletableFuture。
 */
public class AnyOfDemo {
    public static void main(String[] args) {

        CompletableFuture<String> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("第一阶段");
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            return "world";
        });
        CompletableFuture<String> future1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("第二阶段");
            return "hello";
        });
        CompletableFuture<Object> objectCompletableFuture = CompletableFuture.anyOf(future, future1);
        System.out.println(objectCompletableFuture.join());

    }
}
